<template>
  <div>
    <div class="headerbox">
      <div class="header">
        <div class="headerL">
          <a onclick="javascript:history.back(-1)" class="goback"
            ><img
              src="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/goback.png"
          /></a>
        </div>
        <div class="headerC0">
          <div class="ssdiv">
            <input
              name=""
              @keyup="onkeyup"
              v-model="seachName"
              type="text"
              ref="inputs"
              v-focus="true"
              @keyup.enter="searchTo"
              placeholder="华为mate20"
            />
            <a href="#" @click="searchTo"
              ><img
                src="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/ss.png"
            /></a>
          </div>
        </div>
        <div class="headerR">
          <a href="#" @click.prevent="searchTo">搜索</a>
        </div>
      </div>
    </div>
    <div class="hbox"></div>
    <div class="zjssbox">
      <img
        src="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/zuijin.png"
        class="i1"
      />
      <p class="sstxt">最近搜索</p>
      <img
        src="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/del.png"
        class="i2"
      />
    </div>
    <div class="clear"></div>
    <div class="ssbox">
      <div v-for="(item, index) in filterlist" :key="index">
        <router-link
          :to="{ name: 'GoodsResults', params: { pname: item.goodsName } }"
          >{{ item.goodsName }}</router-link
        >
      </div>
      <div
        v-if="limitN != Searchlist.length && Searchlist.length > 10"
        v-on:click="limitN = Searchlist.length"
      >
        <a href="#"><van-icon class="xiangxia" name="play-circle" /></a>
      </div>
      <div v-else v-on:click="limitN = 10">
        <a href="#"><van-icon class="xiangxia_t" name="play-circle" /></a>
      </div>
    </div>
    <div class="clear"></div>
    <div class="zjssbox">
      <img
        src="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/huomiao.png"
        class="i1"
      />
      <p class="sstxt">热门搜索</p>
    </div>
    <div class="clear"></div>
    <div class="ssbox">
      <a
        href="#"
        v-for="item in words.slice(0, 10)"
        :key="item.sid"
        @click.prevent="toResults(item.Words)"
        >{{ item.Words }}</a
      >
    </div>
    <transition name="van-slide-up">
      <div class="PrepBox" v-if="PrepBoxShow">
        <ul>
          <li
            v-for="item in wordsList"
            :key="item.sid"
            class="van-hairline--bottom"
          >
            <router-link
              :to="{ name: 'GoodsResults', params: { pname: item.Words } }"
              >{{ item.Words }}</router-link
            >
          </li>
        </ul>
      </div>
    </transition>
  </div>
</template>
<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      seachName: "",
      PrepBoxShow: false,
      wordsList: [],
      time: null,
      Recent: [],
      words: [],
      Searchlist: [],
      limitN: 10,
    };
  },
  directives: {
    focus: {
      inserted: function (el, { value }) {
        if (value) {
          el.focus();
        }
      },
    },
  },
  mounted() {
    this.unique(this.$store.state.RecentlyGoods);
    this.$nextTick(() => {
      this.$refs.inputs.focus(); //通过 $refs 获取dom 并绑定 focus 方法
    });
  },
  created() {
    this.getwordsRecommended();
  },
  activated() {
    this.unique(this.$store.state.RecentlyGoods);
  },
  methods: {
    unique(arr) {
      // console.log(arr);
      var hash = [];
      for (var i = 0; i < arr.length; i++) {
        if (hash.indexOf(arr[i].name) == -1) {
          hash.unshift({
            goodsName: arr[i].name,
            time: arr[i].time,
          });
        }
      }
      // console.log(hash);
      this.Searchlist = hash;
    },
    //删除历史搜索
    removeItem(item) {
      console.log(item);
    },
    async searchTo() {
      if (this.seachName === "") {
        this.$toast("请输入！");
      } else {
        this.$router.push({
          name: "GoodsResults",
          params: { pname: this.seachName },
        });
        let datas = {
          name: this.seachName,
          time: new Date(),
        };
        this.$store.commit("setRecentlyGoods", datas);
        this.PrepBoxShow = false;
        const { data: res } = await this.$http.get("ValidationWords", {
          params: { words: this.seachName },
        });
        let words = {
          wods: this.seachName,
        };
        this.Recent.push(words);

        if (res.status === 200) {
          this.addWords();
        }
        if (res.status === 202) {
          this.addHotwords();
        }
      }
    },
    async addWords() {
      let data = {
        Words: this.seachName,
        hot: 1,
      };
      const { data: res } = await this.$http.post("addWords", data);
    },
    async addHotwords() {
      let data = {
        Words: this.seachName,
      };
      const { data: res } = await this.$http.post("addHot", data);
    },
    toResults(val) {
      this.$router.push({
        name: "GoodsResults",
        params: { pname: val },
      });
    },
    async onkeyup() {
      if (this.seachName === "") {
        this.PrepBoxShow = false;
      } else {
        this.PrepBoxShow = true;
        //防抖操作
        if (this.time !== null) {
          clearTimeout(this.time);
        }
        this.time = setTimeout(() => {
          this.searchWords();
        }, 200);
      }
    },
    async searchWords() {
      const { data: res } = await this.$http.get("searchWords", {
        params: { title: this.seachName },
      });
      // console.log(res);
      this.wordsList = res.data;
    },
    async getwordsRecommended() {
      const { data: res } = await this.$http.get("wordsRecommended");
      this.words = res.data;
    },
  },
  computed: {
    ...mapState(["RecentSearch"]),
    filterlist: function () {
      return this.Searchlist.slice(0, this.limitN);
    },
  },
};
</script>
<style lang="less" scoped>
.headerbox {
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  .header {
    padding: 0.5rem 0;
    height: 1.8rem;
    position: fixed;
    width: 100%;
    max-width: 640px;
    top: 0;
    background: #fff;
    z-index: 9999;
    border-bottom: 1px solid #eee;
    .headerL {
      position: absolute;
      left: 0;
      top: 0;
      height: 1.8rem;
      padding-left: 0.6rem;
      img {
        display: block;
        height: 1.1rem;
        padding-top: 0.9rem;
      }
    }
    .headerC0 {
      width: 75%;
      margin: 0 auto;
      height: 1.8rem;
      line-height: 1.8rem;
      text-align: center;
      background: #f7f7f7;
      border: 1px solid #f7f7f7;
      box-sizing: border-box;
      border-radius: 4px;
      .ssdiv {
        display: block;
        width: 100%;
        height: 100%;
        border: 1px solid #f47b5d;
        border-radius: 12px;
        input {
          float: left;
          width: 80%;
          border: 0;
          outline: none;
          height: 1.8rem;
          line-height: 1.8rem;
          color: #212121;
          font-size: 0.8rem;
          background: none;
          padding-left: 0.3rem;
          border-radius: 12px;
        }
        img {
          float: right;
          height: 1.8rem;
        }
      }
    }
    .headerR {
      position: absolute;
      right: 0;
      top: 0;
      height: 1.8rem;
      padding-right: 0.6rem;
      a {
        color: #333;
        font-size: 0.8rem;
        line-height: 2.8rem;
      }
    }
  }
}
.zjssbox {
  padding: 0.5rem 0.6rem;
  overflow: hidden;
  height: 1.8rem;
  .i1 {
    float: left;
    display: block;
    height: 1.1rem;
    margin-top: 0.2rem;
  }
  .sstxt {
    height: 1.5rem;
    line-height: 1.7rem;
    float: left;
    color: #9c9c9c;
    font-size: 0.8rem;
  }
  .i2 {
    float: right;
    display: block;
    height: 1.1rem;
    margin-top: 0.2rem;
  }
}
.ssbox {
  overflow: hidden;
  padding: 0 0.6rem;
  a {
    border: 1px solid #ddd;
    display: block;
    float: left;
    height: 1.4rem;
    line-height: 1.4rem;
    padding: 0 0.5rem;
    font-size: 0.75rem;
    margin-right: 0.5rem;
    margin-bottom: 0.5rem;
    border-radius: 15px;
  }
}
.zjssbox {
  padding: 0.5rem 0.6rem;
  overflow: hidden;
  height: 1.8rem;
  .i1 {
    float: left;
    display: block;
    height: 1.1rem;
    margin-top: 0.2rem;
  }
  .sstxt {
    height: 1.8rem;
    line-height: 1.8rem;
    float: left;
  }
}
.ssbox {
  overflow: hidden;
  padding: 0 0.6rem;
  a {
    border: 1px solid #ddd;
    display: block;
    float: left;
    height: 1.4rem;
    line-height: 1.4rem;
    padding: 0 0.5rem;
    font-size: 0.75rem;
    margin-right: 0.5rem;
    margin-bottom: 0.5rem;
    border-radius: 15px;
  }
}
.PrepBox {
  width: 100%;
  height: 100%;
  position: absolute;
  background: #ffffff;
  top: 50px;
  left: 0;
  ul {
    overflow: hidden;
    li {
      padding: 8px;
      text-align: left;
      line-height: 24px;
    }
  }
}
.xiangxia {
  font-size: 18px;
  transform: rotate(90deg);
  line-height: 24px;
}
.xiangxia_t {
  font-size: 18px;
  transform: rotate(-90deg);
  line-height: 24px;
}
</style>